package com.hao.array;

/**
 * TODO
 * 环形队列
 * @author 97472
 * @version 1.0
 * @date 2021/4/15 17:23
 */
public class CircularQueue implements Queue{
    private static final int QUEUE_MAX_SIZE = 500;
    private static final int FRONT_INITIAL_VALUE = 0;
    private static final int REAR_INITIAL_VALUE = 0;
    private static final int SIZE_INITIAL_VALUE = 0;

    private int[] queue = new int[QUEUE_MAX_SIZE];
    private int front = FRONT_INITIAL_VALUE;
    private int rear = REAR_INITIAL_VALUE;
    private int size = SIZE_INITIAL_VALUE;

    public CircularQueue() {
        this(QUEUE_MAX_SIZE);
    }

    public CircularQueue(int maxSize) {
        init(FRONT_INITIAL_VALUE, REAR_INITIAL_VALUE, SIZE_INITIAL_VALUE,maxSize);
    }

    private void init(int front, int rear, int size, int queueSize){
        this.front = front;
        this.rear = rear;
        this.size = size;
        queue = new int[queueSize];
    }

    public void push(int i) {
        if (size <= QUEUE_MAX_SIZE){
            queue[rear] = i;
            rear = (rear+1)%queue.length;
            size++;
        }else {
            throw new RuntimeException("队列已满");
        }
    }

    public int pop() {
        if (size > 0){
            int popValue = queue[front];
            front = (front+1)%queue.length;
            size--;
            return popValue;
        }else {
            throw new RuntimeException("队列为空");
        }
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public int getFront() {
        if (size > 0){
            return queue[front];
        }else {
            throw new RuntimeException("队列为空");
        }
    }
}
